package core.imust.com.imustcore.fragment;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.gson.Gson;

import java.util.HashMap;

import core.imust.com.imustcore.R;
import core.imust.com.imustcore.bean.NewsChannelData;
import core.imust.com.imustcore.fragment.sub.TabChannelFragment;
import core.imust.com.imustcore.net.JrttApi;
import core.imust.com.imustcore.net.OnDataLoadListener;
import core.imust.com.imustcore.net.ResponseData;
import core.imust.com.imustcore.net.RetrofitUtil;
import retrofit2.Call;

/**

 Fragment是一个可以被包含在 FragmentActivity里面的视图
 1，一般我们使用support-v4包里面
 2, 如果你创建一个Activity,配置layout文件，编写逻辑
 得到功能清单里面AndroidManifest.xml里面配置
 一般情况是ok
 如果一个Activity比较复杂，建议大家使用Frgament
 小Activity
 创建一个Fragment,配置layout文件，编写逻辑
 不需要到AndroidManifest.xml里面配置，
 只需要通过FragmentManger,可以添加一个布局
 */

public class NewsChannelFragment extends BaseFragment implements OnDataLoadListener{
    private View view;
    private NewsChannelData data;
    private ViewPager viewPager;
    private TabLayout tabLayout;
    private HashMap<String,TabChannelFragment> pages=new HashMap<>();
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        initView();
        requestNet();
        return view;
    }

    @Override
    public void initView() {
        view=View.inflate(getContext(), R.layout.fragment_news_channel,null);
        //获取指示器
        tabLayout= (TabLayout) view.findViewById(R.id.tab_layout);
        //页面
        viewPager= (ViewPager) view.findViewById(R.id.tab_viewpager);
    }

    @Override
    public void requestNet() {
        JrttApi api= RetrofitUtil.get();
        Call<ResponseData> call=api.getDataNews();
        //调用RetrofitUtil工具
        RetrofitUtil.send(getContext(),call,NewsChannelFragment.this);
    }

    @Override
    public void parsejson(String json) {
        System.out.println(json);
        Gson gson=new Gson();
        //注意：如果服务端返回一个json数据是[]
        //我们给.fromJson传参数  from()
        //{list:...}
        //一个class类对应一个json对象
        String json_="{list:"+json+"}";
        data=gson.fromJson(json_,NewsChannelData.class);
        //遍历一下这个页面多少个标题，一个标题对应一个新闻列表页面（三级），有多少标题就会对应多少个页面
        for (NewsChannelData.ListBean item: data.list){
            //System.out.println(item.title+" "+item.url);
            TabChannelFragment tabChannelFragment = new TabChannelFragment();
            tabChannelFragment.setUrl(item.url);//每个页面都有访问地址
            pages.put(item.title,tabChannelFragment);

        }
        //System.out.println(data.list.size());
    }

    @Override
    public void showData() {
        /*for (NewsChannelData.ListBean item:data.list){
            System.out.println(item.title);
        }*/
        //1、展示出来一个ViewPager
        NewAdapter adapter=new NewAdapter(getFragmentManager());
        //只要给viewPager设置一个适配器，它就可以产生多个子页面，可以滑动切换页面
        viewPager.setAdapter(adapter);
        //tabLayout可以只是页面叫什么名字，同时也可以指定页面属于第几个
        //用法非常简单，只要将有adapter的viewPager绑定给tabLayout
        tabLayout.setupWithViewPager(viewPager);
    }
    class NewAdapter extends FragmentPagerAdapter{

        public NewAdapter(FragmentManager fm) {
            super(fm);
        }
        //CharSequence字符序列，字符串
        @Override
        public String getPageTitle(int position) {
            NewsChannelData.ListBean bean=data.list.get(position);
            return bean.title;
        }

        @Override
        public Fragment getItem(int position) {
            /*SubFragment f=new SubFragment();
            NewsChannelData.ListBean bean=data.list.get(position);
            f.setTitle(bean.title);
            return f;*/
            NewsChannelData.ListBean newBean=data.list.get(position);
            TabChannelFragment tabChannelFragment=pages.get(newBean.title);
            return tabChannelFragment;
        }

        @Override
        public int getCount() {
            return data.list.size();
        }
    }
}
